package com.cennavi.minenavidemo.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.widget.ColorTextView;
import com.minedata.minenavi.poiquery.Tip;

import java.util.List;

/**
 * 联想搜索Adapter
 */
public class InputtipsAdapter extends RecyclerView.Adapter<InputtipsAdapter.MyViewHolder> {
    private String key;
    private List<Tip> list;
    private PoiSelectListener listener;
    private Context mContext;

    public InputtipsAdapter(Context context, List<Tip> list) {
        this.list = list;
        this.mContext = context;
    }

    public void setListener(PoiSelectListener listener) {
        this.listener = listener;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inputtips_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final Tip tip = list.get(position);

        if (!TextUtils.isEmpty(key)) {
            holder.name.setSpecifiedTextsColor(tip.getName(), new String[]{key}, Color.BLUE);
        } else {
            holder.name.setText(tip.getName());
        }

        holder.address.setText(tip.getAddress());
        holder.content.setOnClickListener(v -> {
            if (listener != null) {
                listener.onPoiSelect(tip);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private ColorTextView name;
        private TextView address;
        private View content;

        public MyViewHolder(View itemView) {
            super(itemView);
            name = (ColorTextView) itemView.findViewById(R.id.name);
            address = (TextView) itemView.findViewById(R.id.address);
            content = itemView.findViewById(R.id.poi_item_layout);
        }
    }

    public interface PoiSelectListener {

        void onPoiSelect(Tip info);
    }
}
